﻿using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Xpedite.Extensions;
using Xpedite.Resources;
using Xpedite.Resources.Definitions;
using Xpedite.WebAbstraction;

/* Copyright (c) 2010 CBaxter
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), 
 * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 
 * IN THE SOFTWARE. 
 */

namespace Xpedite.Test.Extensions
{
  [TestClass]
  public class WebPageExtensionsTest
  {
    private readonly Mock<IWebContext> _webContext = new Mock<IWebContext>(MockBehavior.Strict);
    private readonly Mock<IWebPage> _webPage = new Mock<IWebPage>(MockBehavior.Strict);

    [TestMethod]
    public void TestGetResourceContextWhereDoesntExist()
    {
      var factory = new Mock<IResourceDefinitionFactory>(MockBehavior.Strict);
      var items = new Dictionary<Object, Object>();

      _webPage.SetupGet(mock => mock.Items).Returns(items);
      _webPage.SetupGet(mock => mock.Context).Returns(_webContext.Object);

      _webContext.Setup(mock => mock[typeof(ResourceDefinitionFactory).AssemblyQualifiedName]).Returns(factory.Object);

      Assert.IsNotNull(_webPage.Object.GetResourceContext());
    }

    [TestMethod]
    public void TestGetResourceContextWhereAlreadyExists()
    {
      var context = new Mock<IResourceContext>(MockBehavior.Strict);
      var items = new Dictionary<Object, Object> { { typeof(IResourceContext).AssemblyQualifiedName, context.Object } };

      _webPage.SetupGet(mock => mock.Items).Returns(items);

      Assert.AreSame(context.Object, _webPage.Object.GetResourceContext());
    }
  }
}
